//
//  SecenddViewController.m
//  SelectionForYou
//
//  Created by qianfeng on 15-6-11.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "SecenddViewController.h"
#import <WebKit/WebKit.h>
#import "MMProgressHUD.h"
#import "MMLinearProgressView.h"
#import "MyControl.h"
#import "MyModel.h"
#import "DBManager.h"
#import "SubModel.h"
#define kSubUrl @"http://jingxuan.guokr.com/pick/v2/%ld/"
@interface SecenddViewController ()<WKNavigationDelegate,UIActionSheetDelegate>
{
   
    WKWebView *_webView;
}

@property(nonatomic,strong) UIBarButtonItem *ietm2;
@property(nonatomic,strong) MyModel *model;
@property(nonatomic,strong) NSTimer *time;
@end

@implementation SecenddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _model=[[MyModel alloc] init];
    _dataArr=[[NSArray alloc ] init];
    UIButton *button=[MyControl creatButtonWithFrame:CGRectMake(kScreenSize.width-100, 0, 40, 40) target:self sel:@selector(btnClick:) tag:101 image:nil  title:@"分享"];
    [button setTintColor:[UIColor redColor]];
    UIBarButtonItem *ietm=[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAction target:self action:@selector(btnClick:)];
    ietm.tag=102;
    UIImage *image=[[UIImage imageNamed: @"002"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.ietm2=[[UIBarButtonItem alloc] initWithImage:image  style:UIBarButtonItemStylePlain target:self action:@selector(btnClick:)];
    _ietm2.tag=103;
    UIBarButtonItem *item3=[[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    self.navigationItem.rightBarButtonItems = @[ietm,item3,_ietm2];
    [self creatWebView];
    
    
    
    
    
    
}
- (void)btnClick:(UIButton *)btn{
    
    switch (btn.tag) {
        case 101:
        {
            NSLog(@"第一个按钮");
            UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"短信",@"邮件",@"打电话", nil];
            
            [sheet showInView:self.view];

        }
            break;
        case 102:
        {
             NSLog(@"第二个按钮");
            UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"短信",@"邮件",@"打电话", nil];
            
            [sheet showInView:self.view];

        }
            break;
        case 103:
            
        {
            // id,headline_img_tb,title,url
            
            NSLog(@"%@",_titleHose);
            _dataArr=@[_headline_img_tb,_titleHose,_link_v2,_idd];
            
             NSLog(@"第三个按钮");//收藏
           NSLog(@"%@",_dataArr);
            NSLog(@"%@",self.idd);
            [[DBManager sharedManager] insertModel: _dataArr  recordType:@"favorites"];
          
            self.ietm2.enabled=NO;
            UIImage *image=[[UIImage imageNamed: @"001"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.ietm2 setImage:image   ];
            
           
            
        }
            break;
            
        default:
            break;
    }
    
  
    
    
    
    
}
- (void)creatWebView{
    self.automaticallyAdjustsScrollViewInsets=NO;
    _webView=[[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64)];
    _webView.navigationDelegate=self;
   // _webView.allowsBackForwardNavigationGestures=YES;
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSubUrl,self.idd.integerValue]]];
    // NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://jingxuan.guokr.com/pick/v2/15025/"]];
    [_webView loadRequest:request];
   
    [self.view addSubview:_webView];
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    if (!self.time) {
        self.time=[NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(timerClick:) userInfo:nil repeats:YES];
    }
    [MMProgressHUD setProgressViewClass:[MMLinearProgressView class]];
    [MMProgressHUD showDeterminateProgressWithTitle:@"Load..." status:nil];
}
- (void)timerClick:(NSTimer *)timer{
    static NSInteger a;
    if (!a) {
        [MMProgressHUD updateProgress:_webView.estimatedProgress];
    }else{
        return;
    }
    

    
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    if (self.time) {
        [self.time invalidate];
        self.time=nil;
        [MMProgressHUD dismissWithSuccess:@"加载完成" title:@"好厉害"];
    }


}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    if (self.time) {
        [self.time invalidate];
        self.time=nil;
        [MMProgressHUD dismissWithError:@"下载失败" title:@"加油哦"];
    }
    


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
